package com.zhudeshuai.zhudemolist.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.zhu.zhuCore.utils.ContextProvider;
import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.bean.realm_bean.Human;
import com.zhudeshuai.zhudemolist.util.ToastUtil;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

/**
 * Realm使用
 *
 * @author zhudeshuai
 * @since 16/4/15
 */
public class RealmDemoActivity extends Activity {

    private static final String TAG = "RealmDemoActivity";
    private Realm mRealm;
    private TextView resultTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_realm_demo);
        resultTv = (TextView) findViewById(R.id.tv_result);
        mRealm = Realm.getInstance(new RealmConfiguration.Builder(ContextProvider.getApplicationContext())
                .name("TestRealm.realm").build());
    }


    /**
     * 插入数据
     *
     * @param view
     */
    public void insertData(View view) {


        //方式1
        Human bean = new Human();
        bean.setName("张三");
        bean.setAge(23);

        mRealm.beginTransaction();
        mRealm.copyToRealm(bean);
        mRealm.commitTransaction();


    }

    /**
     * 查询数据
     *
     * @param view
     */
    public void searchData(View view) {
        resultTv.setText("");
        RealmResults<Human> all = mRealm.where(Human.class).findAll();
        Log.i(TAG, all == null ? "0" : String.valueOf(all.size()));
        if (all != null) {
            for (Human human : all) {
                resultTv.append("name:" + human.getName() +
                        ",age:" + human.getAge() + "\r\n");
            }
        }
    }

    /**
     * 异步查询数据
     *
     * @param view
     */
    public void asyncQuery(View view) {

        mRealm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<Human> humans = realm.where(Human.class).equalTo("age", 18).findAll();

                humans.get(0).setAge(19);

            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                ToastUtil.showToast("success");
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                ToastUtil.showToast(error.toString());
            }
        });
    }

    public void clearData(View view) {
        mRealm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.delete(Human.class);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                ToastUtil.showToast("清除成功");
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                ToastUtil.showToast("清除失败");
            }
        });
    }

    public static void open(Activity activity) {
        activity.startActivity(new Intent(activity, RealmDemoActivity.class));
    }


}
